/**
 * 
 */
package testy.core;

import static org.junit.Assert.*;

import java.io.IOException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import baduga.client.Network_Api;
import baduga.core.Core_JsonDataSchema;
import baduga.core.Core_Network_Api;

/**
 * @author Janek
 *
 */
public class Test_Core_Network_API {
	
	
	/**
	 * String Json
	 * 
	 * [0] - Encode
	 * [1] - Decode
	 */
	String jsonResult[];
	
	
	/**
	 * Data Json
	 * 
	 * [0] - Encode
	 * [1] - Decode
	 */
	Core_JsonDataSchema jsonDataResult[];
	
	
	
	Network_Api nObj = new Network_Api();
	

	/**
	 * Inicjalizacja
	 * 
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		
		
		jsonDataResult = new Core_JsonDataSchema[2];
		
		
		/** JSON object */
		
		//#1
		jsonDataResult[0] = new Core_JsonDataSchema(0x000, new int[] {
			0x0000, 0x0001
		}, new Object[] {
			"D1_Test1", "D1_Test2"	
		}, new Object[][] {
			
			new Object[] {
				"D2_Test1", "D2_Test2"	
			},
			null
		});
		
		
		
		//#2
		jsonDataResult[1] = new Core_JsonDataSchema(0x000, new int[] {
			0x0000, 0x0001, 0x00FF
		}, new Object[] {
			"D1C_Test1", "D1C_Test2"	
		}, new Object[][] {
			null,
			new Object[] {
				"D2V_Test1", "D2E_Test2"	
			}

		});
		
		
		/** Stringi */
		jsonResult = new String[2];
		
		jsonResult[0] = "{\"kod\":0,\"args\":[0,1],\"dataD1\":[\"D1_Test1\",\"D1_Test2\"],\"dataD2\":[[\"D2_Test1\",\"D2_Test2\"],null]}";
		jsonResult[1] = "{\"kod\":0,\"args\":[0,1,255],\"dataD1\":[\"D1C_Test1\",\"D1C_Test2\"],\"dataD2\":[null,[\"D2V_Test1\",\"D2E_Test2\"]]}";
		
		
		
	}

	/**
	 * Czyszczenie
	 * 
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
		//jsonResult = null;
		//jsonDataResult = null;
		
	}

	/**
	 * Kodowanie z Object -> String
	 * 
	 * Test method for {@link baduga.core.Core_Network_Api#encodeJson(baduga.core.Core_JsonDataSchema)}.
	 */
	@Test
	public final void testEncodeJson() {
		String result = "";
		
		try {
			result = nObj.encodeJson( jsonDataResult[0] );
		} catch (IOException e) {

			fail("#0 - EncodeJsoin IO - FAIL " + e.getMessage() );
		}
		
		
		
		assertEquals("Czy output odpowiada wersji kontrolnej (O -> S)?", result, jsonResult[0]);
	}

	/**
	 * Kodowanie z String -> Object
	 * 
	 * Test method for {@link baduga.core.Core_Network_Api#decodeJson(java.lang.String)}.
	 */
	@Test
	public final void testDecodeJson() {
		Core_JsonDataSchema result = null;
		String result2 = "";
		

		result = nObj.decodeJson( jsonResult[1] );

		
		try {
			result2 = nObj.encodeJson( result );
		} catch (IOException e) {

			fail("#1 - EncodeJsoin IO - FAIL " + e.getMessage() );
		}
		
		assertEquals("Czy output odpowiada wersji kontrolnej (S -> O)?", result2, jsonResult[1]);
	}

}
